<?php
/*2017-4-10 15.45 jack*/
namespace app\api\model;
use plugin\filter;
use think\Model;
use app\api;
use think\Session;

/*基类模型*/
class CommonModel extends Model
{
    protected $session_prefix = 'api';
    //商家id
    protected $seller_id ;
    //商家基本信息
    protected $sellerInfo;
    //筛选条件
    protected $field_where ;
    //默认显示字段
    protected $default_fields;
    //默认字段的过滤规则
    protected $filter_field = [];
    //默认排序
    protected $default_sort;
    //用户id
    protected $user_id;
    //用户名
    protected $user_name;
    //用户匿名
    protected $nickname;
    //是否客服
    protected $isKf;

    public function __construct($args = [])
    {
        $args = $this->merge_args($args);

        //用户id
        $this->user_id   = $args['user_id'];
        //用户名
        $this->user_name = $args['user_name'];
        //用户匿名
        $this->nickname  = $args['nickname'];
        //是否客服
        $this->isKf      = $args['isKf'];

    }

    protected function merge_args($args)
    {
        $default = [
            'user_id'       => 0,
            'user_name'     => "匿名",
            'nickname'      => "匿名",
            "isKf"            => 0
        ];

        return array_merge($default,$args);
    }



    /** 检查登录状态
     * @return bool
     */
    protected function check_login()
    {
        return !empty($this->user_id);
    }

    /**
     * 清空session
     */
    protected function clear_session()
    {
        Session::clear($this->session_prefix);
    }

    protected function getUserid($is_enc = false)
    {
        return $is_enc ? $this->EncUserid($this->user_id) : $this->user_id;
    }

    protected function EncUserid($client_id)
    {
        return sha1($client_id);
    }

    //设置当前用户是否在线，用lineState键值识别
    public function setOnLineState($arr,$OnLineArr)
    {
        if (empty($arr)) return [];
        foreach ($arr as $k => $v) {
            $arr[$k]['lineState'] = !empty($OnLineArr) && in_array($v['enc_client_id'], $OnLineArr)
                ? 1 : 0;
        }
        return $arr;
    }


}